#include "CarriageValue.h"
#include "IniFile.h"
#include "TStringUtil.h"

namespace details {

}

static bool readIntPair(comn::IniFile& iniFile, const char* section, const char* key, IntPair& intPair)
{
    std::string value;
    if (!iniFile.queryValue(section, key, value))
    {
        return false;
    }

    comn::StringUtil::split(value, '-', intPair.first, intPair.second);

    return true;
}





CarriageValue::CarriageValue():
    exception()
{
    reset();
}

bool CarriageValue::load(const char* filepath)
{
    comn::IniFile iniFile;
    if (!iniFile.load(filepath))
    {
        return false;
    }

    iniFile.queryValue("Carriage", "Exception", exception);

    readIntPair(iniFile, "Carriage", "Empty", empty);
    readIntPair(iniFile, "Carriage", "Full", full);
    readIntPair(iniFile, "Carriage", "Crowd", crowd);
    readIntPair(iniFile, "Carriage", "VeryCrowd", veryCrowd);

    return true;
}

void CarriageValue::reset()
{
    exception = 6;
    empty = IntPair(0,3);
    full = IntPair(4,6);
    crowd = IntPair(7,8);
    veryCrowd = IntPair(9,MAX_PEOPLE_COUNT);
}

CarriageValue::Status CarriageValue::getStatus(int count)
{
    Status status = kEmpty;
    if (count >= veryCrowd.first)
    {
        status = kVeryCrowd;
    }
    else if (count >= crowd.first)
    {
        status = kCrowd;
    }
    else if (count >= full.first)
    {
        status = kFull;
    }
    return status;
}
